package com.swim.common.utils;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @program     : swim
 * @description : 随机生成字符工具类
 * @author      : 刘军武
 * @version     : 1.0  
 * @create      : 2019年12月6日 下午2:23:07
 **/
public class RandomGenerator {
    /***/
    private static final char[] DEFAULT_CODEC = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();

    private static final char[] NUMBER_CODEC = "1234567890".toCharArray();

    /** Random */
    private static Random random = new SecureRandom();

    private Random numberRandom = new Random();

    /** Length of Random*/
    private int                 length;

    /**
     * 构造方法
     */
    public RandomGenerator() {
        this(6);
    }

    /**
     * 构造方法
     * @param length
     */
    public RandomGenerator(int length) {
        this.length = length;
    }

    /**
     * 生成随机字符串
     *
     * @return
     */
    public String generate() {
        byte[] verifierBytes = new byte[length];
        random.nextBytes(verifierBytes);
        return getAuthorizationCodeString(verifierBytes);
    }


    /**
     * 生成随机字符串
     *
     * @return
     */
    public String generateNum() {
        byte[] verifierBytes = new byte[length];
        random.nextBytes(verifierBytes);
        return getNumCodeString(verifierBytes);
    }

    /**
     * 生成授权码
     *
     * @param verifierBytes
     * @return
     */
    protected String getAuthorizationCodeString(byte[] verifierBytes) {
        char[] chars = new char[verifierBytes.length];
        for (int i = 0; i < verifierBytes.length; i++) {
            chars[i] = DEFAULT_CODEC[((verifierBytes[i] & 0xFF) % DEFAULT_CODEC.length)];
        }
        return new String(chars);
    }


    /**
     * 生成授权码
     *
     * @param verifierBytes
     * @return
     */
    protected String getNumCodeString(byte[] verifierBytes) {
        char[] chars = new char[verifierBytes.length];
        for (int i = 0; i < verifierBytes.length; i++) {
            chars[i] = NUMBER_CODEC[((verifierBytes[i] & 0xFF) % NUMBER_CODEC.length)];
        }
        return new String(chars);
    }

    public String getVolumeBatchString(Long shopId){
        Date nowTime = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String resultStr = sdf.format(nowTime);
        resultStr = resultStr + shopId + generate();//规则时间+店铺id+随机字符串
        return resultStr;
    }

    /**
     * Setter for Random
     * @param random
     */
    public void setRandom(Random random) {
        this.random = random;
    }

    /**
     * Setter for length
     *
     * @param length
     */
    public void setLength(int length) {
        this.length = length;
    }

    /**
     * 生成0-100中间的随机两位数字
     * @return
     */
    public String get2randomNumber(){
        int ends = random.nextInt(99);
        String result = String.format("%02d",ends);
        return result;
    }
    /**
     * 生成0-99999999中间的随机八位数字
     * @return
     */   
    public static String get8randomNumber(){
    	int ends = random.nextInt(99999999);
    	String result = String.format("%08d",ends);
    	return result;
    }
    
    public String get2Number(Integer orderNumber){
        String result = String.format("%02d",orderNumber);
        return result;
    }

    public String get3Number(Integer orderNumber){
        String result = String.format("%03d",orderNumber);
        return result;
    }

    public String get4Number(Integer orderNumber){
        String result = String.format("%04d",orderNumber);
        return result;
    }

    public String get5Number(Integer orderNumber){
        String result = String.format("%05d",orderNumber);
        return result;
    }


    public static void main(String[] args) {
    	RandomGenerator randomGenerator = new RandomGenerator();
    	String result = randomGenerator.get8randomNumber();
    	System.out.println(result);
	}

}
